import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/page/clouds/component/triangle_up_painter.dart';
import 'package:full_display/unit/asset_utils/asset_utils.dart';
import 'package:full_display/unit/screen/screen_util1.dart';
import 'package:full_display/unit/screen/screen_utility.dart';

/// 控件的key
GlobalKey _anchorKey = GlobalKey();
typedef ClickCallBack = void Function(int selectIndex, Map selectText);

class PopMenus {
  static void showPop(
      {required BuildContext context,
      required List listData,
      required String selText,
      required bool isImg,
      required double itemHeight,
      required double itemWidth,
      required double topH,
      required double leftP,
      required double trianglePosition,
      required ClickCallBack clickCallback}) {
    Widget _buildMenuLineCell(dataArr) {
      return ListView.separated(
        itemCount: dataArr.length,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (BuildContext context, int index) {
          return GestureDetector(
              onTap: () {
                Navigator.pop(context);
                if (clickCallback != null) {
                  clickCallback(index, listData[index]);
                }
              },
              child: Container(
                height: itemHeight,
                width: itemWidth,
                color: ColorPlate.backgroundOpacity0,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    selText != dataArr[index]['title']
                        ? Container(
                            padding:
                                EdgeInsets.only(left: 15.0.H, right: 15.0.H),
                            height: itemHeight,
                            width: itemWidth,
                            color: ColorPlate.backgroundOpacity0,
                            child: isImg
                                ? Row(
                                    children: [
                                      Image.asset(
                                        width: 16.0.W,
                                        height: 16.0.H,
                                        AssetUtils.getAsset2Image(
                                            dataArr[index]['img']),
                                      ),
                                      Container(
                                        width: 12.0.H,
                                      ),
                                      Text(
                                        dataArr[index]['title'],
                                        textAlign: TextAlign.center,
                                        style: const TextStyle(
                                            fontSize: 16, color: Colors.white),
                                      ),
                                    ],
                                  )
                                : Row(
                                    children: [
                                      Text(
                                        dataArr[index]['title'],
                                        textAlign: TextAlign.center,
                                        style: const TextStyle(
                                            fontSize: 16, color: Colors.white),
                                      ),
                                    ],
                                  ),
                          )
                        : Container(
                            padding:
                                EdgeInsets.only(left: 15.0.H, right: 15.0.H),
                            height: itemHeight,
                            width: itemWidth,
                            color: ColorPlate.backgroundOpacity0,
                            child: Row(
                              children: [
                                Text(
                                  dataArr[index]['title'],
                                  textAlign: TextAlign.center,
                                  style: const TextStyle(
                                      fontSize: 16,
                                      color: ColorPlate.mainBlack),
                                ),
                              ],
                            ),
                          ),
                  ],
                ),
              ));
        },
        separatorBuilder: (context, index) {
          return const Divider(
            height: 0.1,
            color: Color(0xFFE6E6E6),
          );
        },
      );
    }

    _buildMenusView(dataArr) {
      var cellH = dataArr.length * itemHeight;
      return Positioned(
        left: leftP,
        top: topH - 10,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: <Widget>[
            Container(
              padding: EdgeInsets.only(right: trianglePosition),
              child: TriangleUpWidget(height: 10, width: 14),
            ),
            Container(
              decoration: const BoxDecoration(
                gradient: LinearGradient(
                    //渐变位置
                    begin: Alignment.topCenter, //右上
                    end: Alignment.bottomCenter, //左下
                    //渐变颜色[始点颜色, 结束颜色]
                    colors: [ColorPlate.app_blue, ColorPlate.sub_blue]),
                borderRadius: BorderRadius.all(Radius.circular(8.0)),
              ),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(2),
                child: Container(
                  color: ColorPlate.backgroundOpacity0,
                  width: itemWidth,
                  height: cellH,
                  child: _buildMenuLineCell(dataArr),
                ),
              ),
            ),
          ],
        ),
      );
    }

    showDialog(
        context: context,
        barrierDismissible: true,
        builder: (context) {
          return BasePopMenus(child: _buildMenusView(listData));
        });
  }
}

class BasePopMenus extends Dialog {
  BasePopMenus({
    Key? key,
    required this.child,
  }) : super(key: key);

  final Widget child;

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: Stack(
        fit: StackFit.expand,
        children: <Widget>[
          GestureDetector(onTap: () => Navigator.pop(context)),
          child
        ],
      ),
    );
  }
}
